package com.glory.wxelectricitypda.activity;



import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.FinishedProductInspectionAdapter;
import com.glory.wxelectricitypda.adapter.UnpackagedAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BoxLabelReviewBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class FinishedProductInspectionActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.containerNum)
    FreeUI_ClearEditText containerNum;
    @BindView(R.id.boxNum)
    FreeUI_ClearEditText boxNum;
    @BindView(R.id.rbOK)
    RadioButton rbOK;
    @BindView(R.id.returnRecycler)
    RecyclerView returnRecycler;
    public FinishedProductInspectionAdapter mUnpackagedAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_finished_product_inspection;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("成品检验");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etUser.getText().toString().isEmpty()){
                    showToast("请输入工号",false);
                    return;
                }
                if (containerNum.getText().toString().isEmpty()){
                    showToast("请扫描箱号",false);
                    return;
                }
                if (boxNum.getText().toString().isEmpty()){
                    showToast("请扫描盒号",false);
                    return;
                }
                submit();
            }
        });

        returnRecycler.setLayoutManager(new LinearLayoutManager(FinishedProductInspectionActivity.this));
        returnRecycler.setItemAnimator(new DefaultItemAnimator());
        returnRecycler.addItemDecoration(new DividerItemDecoration(FinishedProductInspectionActivity.this, 1));
        mUnpackagedAdapter = new FinishedProductInspectionAdapter(null);
        mUnpackagedAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mUnpackagedAdapter.isFirstOnly(true);
        returnRecycler.setAdapter(mUnpackagedAdapter);

    }

    @Override
    protected void initData() {

        initEdit(containerNum,s->{

            if (etUser.getText().toString().isEmpty()){
                showToast("请输入工号",false);
                return;
            }
            queryBoxLabelReview();
        });


    }

    private void queryBoxLabelReview() {


        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "QUERYCHILDS");
            map.put("PARENTPACKLOTID", containerNum.getText().toString());
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.LABELCHECK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BoxLabelReviewBean>>(
                new TypeToken<ResponseBean<BoxLabelReviewBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BoxLabelReviewBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (response.body().getBody().getXPACKAGEDETAILS().size()>0){
                        mUnpackagedAdapter.setNewData(response.body().getBody().getXPACKAGEDETAILS());
                    }else {
                        mUnpackagedAdapter.setNewData(null);
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<BoxLabelReviewBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LABELCHECK");
            }
        });


    }


    private void submit(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "APPROVEQC");
            map.put("PARENTPACKLOTID", containerNum.getText().toString());
            map.put("CHECKPACKLOTID", boxNum.getText().toString());
            map.put("CHECKRESULT", rbOK.isChecked()?"OK":"NG");
            map.put("CHECKTYPE", "BOX");
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.LABELCHECK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BoxLabelReviewBean>>(
                new TypeToken<ResponseBean<BoxLabelReviewBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BoxLabelReviewBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    showToast("提交成功",false);
                    mUnpackagedAdapter.setNewData(null);
                    containerNum.setText("");
                    boxNum.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<BoxLabelReviewBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LABELCHECK");
            }
        });



    }



    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header",llMain);
    }
}